package com.song.druid.config;

import com.song.druid.annotation.ParamRegex;
import org.apache.commons.lang3.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 验证是否满足正则表达式
 */
public class RegexValidator implements ConstraintValidator<ParamRegex, String> {
    public String regex;

    @Override
    public void initialize(ParamRegex constraintAnnotation) {
        regex = constraintAnnotation.pattern();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (ObjectUtils.isNotEmpty(value)){
            if (!value.matches(regex)){
                return false;
            }
        }
        return true;
    }
}
